package cn.imokay.controller;

import cn.imokay.entities.People;
import cn.imokay.entities.Pet;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HelloController {

    @RequestMapping("hello")
    public String hello(){
        return "hello";
    }

    @RequestMapping(value = "test",method = RequestMethod.GET)
    public String get(@RequestParam String name){
        System.out.println(name);
        return "hello get";
    }

    @RequestMapping(value = "test",method = RequestMethod.POST)
    public String post(@RequestParam String name){
        System.out.println(name);
        return "hello post";
    }

    @RequestMapping(value = "test",method = RequestMethod.PUT)
    public String put(@RequestParam String name){
        System.out.println(name);
        return "hello put";
    }

    @RequestMapping(value = "test",method = RequestMethod.DELETE)
    public String delete(@RequestParam String name){
        System.out.println(name);
        return "hello delete";
    }

    //http://localhost:8080/get/湖北第二师范/大四/物流/王孟震?age=25&girlfriend=陈文丽
    @GetMapping (value = "get/{school}/{grade}/{class}/{name}")
    public Map getSth(@PathVariable String school,
                        @PathVariable Map<String,String> pv,
                        @RequestHeader Map<String,String> hv,
                        @RequestParam String age,
                        @RequestParam String girlfriend,
                        @RequestParam Map<String,String> rv,
                        @CookieValue("_ga") Cookie cookie){
        HashMap<String, Object> map = new HashMap<>();
        map.put("school",school);
        map.put("PathVariable",pv);
        map.put("header",hv);
        map.put("age",age);
        map.put("girlfriend",girlfriend);
        map.put("RequestParam",rv);
        map.put("cookie",cookie);
        //map.put("cookie",cookie);

        return map;
    }

    @PostMapping  (value = "post/{school}/{grade}/{class}/{name}")
    public Map postSth(@PathVariable String school,
                      @PathVariable Map<String,String> pv,
                      @RequestHeader Map<String,String> hv,
                      @RequestParam String age,
                      @RequestParam String girlfriend,
                      @RequestParam Map<String,String> rv,
                      @CookieValue("_ga") Cookie cookie,
                       @RequestBody String rb,
                       @RequestAttribute Map<String,String> reqAttr){
        HashMap<String, Object> map = new HashMap<>();
        map.put("school",school);
        map.put("PathVariable",pv);
        map.put("header",hv);
        map.put("age",age);
        map.put("girlfriend",girlfriend);
        map.put("RequestParam",rv);
        map.put("cookie",cookie);
        map.put("rb",rb);
        map.put("reqAttr",reqAttr);
        //map.put("cookie",cookie);

        return map;
    }


    //矩阵变量
    //UrlPathHelper.removeSemicolonContent默认是true,
    // 表示去掉路径中带分号后面的全部内容,所以不支持矩阵变量,需要手动配置
    //http://localhost:8080/matrix;name=wangmz;habit=climb,ride;gf=cwl/msg
    @GetMapping("{matrix}/msg")
    public Map postMatrix(@MatrixVariable(pathVar = "matrix") String name,
                          @MatrixVariable Map<String,String> matrixMap,
                          @MatrixVariable List<String> habit,
                          @PathVariable("matrix") String matrix){

        HashMap<String, Object> map = new HashMap<>();

        map.put("name",name);
        map.put("matrixMap",matrixMap);
        map.put("matrix",matrix);
        map.put("habit",habit);

        return  map;
    }

    //自定义
    @PostMapping("savePeople")
    public Map postMatrix(People people){

        HashMap<String, Object> map = new HashMap<>();

        map.put("people",people);
        return  map;
    }
}
